package org.example.back.controller.advice;

import org.example.back.service.advice.ChronicDiseaseAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;

import java.util.Map;

@RestController
@RequestMapping("/health/chronic-disease/advice")
public class ChronicDiseaseAdviceController {

    @Autowired
    private ChronicDiseaseAdviceService chronicDiseaseAdviceService;

    // 获取单个慢性病的日常建议
    @GetMapping("/daily/{diseaseId}")
    public Map<String, Object> getDailyAdvice(@PathVariable Integer diseaseId, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        String advice = chronicDiseaseAdviceService.getDailyAdvice(userId, diseaseId);
        return Map.of("success", true, "data", advice);
    }

    // 获取用户所有慢性病的综合日常建议
    @GetMapping("/daily/comprehensive")
    public Map<String, Object> getComprehensiveDailyAdvice(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        String advice = chronicDiseaseAdviceService.getComprehensiveDailyAdvice(userId);
        return Map.of("success", true, "data", advice);
    }

    // 获取单个慢性病的每周建议
    @GetMapping("/weekly/{diseaseId}")
    public Map<String, Object> getWeeklyAdvice(@PathVariable Integer diseaseId, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        String advice = chronicDiseaseAdviceService.getWeeklyAdvice(userId, diseaseId);
        return Map.of("success", true, "data", advice);
    }

    // 获取用户所有慢性病的综合每周建议
    @GetMapping("/weekly/comprehensive")
    public Map<String, Object> getComprehensiveWeeklyAdvice(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        String advice = chronicDiseaseAdviceService.getComprehensiveWeeklyAdvice(userId);
        return Map.of("success", true, "data", advice);
    }

    // 获取单个慢性病的每月建议
    @GetMapping("/monthly/{diseaseId}")
    public Map<String, Object> getMonthlyAdvice(@PathVariable Integer diseaseId, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        String advice = chronicDiseaseAdviceService.getMonthlyAdvice(userId, diseaseId);
        return Map.of("success", true, "data", advice);
    }

    // 获取用户所有慢性病的综合每月建议
    @GetMapping("/monthly/comprehensive")
    public Map<String, Object> getComprehensiveMonthlyAdvice(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        String advice = chronicDiseaseAdviceService.getComprehensiveMonthlyAdvice(userId);
        return Map.of("success", true, "data", advice);
    }

    // 清除指定疾病的建议缓存
    @DeleteMapping("/cache/clear/{diseaseId}")
    public Map<String, Object> clearDiseaseAdviceCache(@PathVariable Integer diseaseId, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        chronicDiseaseAdviceService.cleanUserDiseaseAdviceCache(userId, diseaseId);
        return Map.of("success", true, "message", "建议缓存已清除");
    }

    // 清除用户所有建议缓存
    @DeleteMapping("/cache/clear-all")
    public Map<String, Object> clearAllAdviceCache(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }

        chronicDiseaseAdviceService.cleanAllUserAdviceCache(userId);
        return Map.of("success", true, "message", "所有建议缓存已清除");
    }
}